package com.bjpowernode.services.impls;

import cn.hutool.core.util.NumberUtil;
import com.bjpowernode.mappers.TActivityMapper;
import com.bjpowernode.mappers.TClueMapper;
import com.bjpowernode.mappers.TCustomerMapper;
import com.bjpowernode.mappers.TTranMapper;
import com.bjpowernode.querys.NameValueDataQuery;
import com.bjpowernode.querys.SummaryDataQuery;
import com.bjpowernode.services.StatisticService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

@Service
public class StatisticServiceImpl implements StatisticService {
    @Resource
    private TActivityMapper tActivityMapper;
    @Resource
    private TClueMapper tClueMapper;
    @Resource
    TCustomerMapper tCustomerMapper;
    @Resource
    TTranMapper tTranMapper;

    @Override
    public SummaryDataQuery getSummaryData() {
        Integer goingActivityCount = tActivityMapper.selectGoingActivityCount();
        Integer totalActivityCount = tActivityMapper.selectTotalActivityCount();
        Integer totalClueCount = tClueMapper.selectTotalClueCount();
        Integer totalCustomerCount = tCustomerMapper.selectTotalCustomerCount();
        BigDecimal successTranAmount = tTranMapper.selectSuccessTranAmount();
        BigDecimal totalTranAmount = tTranMapper.selectTotalTranAmount();
        return SummaryDataQuery.builder()
                .goingActivityCount(goingActivityCount)
                .totalActivityCount(totalActivityCount)
                .totalClueCount(totalClueCount)
                .totalCustomerCount(totalCustomerCount)
                .successTranAmount(successTranAmount)
                .totalTranAmount(totalTranAmount)
                .build();
    }

    @Override
    public List<NameValueDataQuery> getSaleFunnelData() {
        List<NameValueDataQuery> nameValueDataQueryList = new ArrayList<>();
        Integer totalClueCount = tClueMapper.selectTotalClueCount();
        BigDecimal clueNum = NumberUtil.div(new BigDecimal(totalClueCount), new BigDecimal(totalClueCount), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
        NameValueDataQuery clueData = NameValueDataQuery.builder()
                .name("线索")
                .value(clueNum)
                .build();
        nameValueDataQueryList.add(clueData);
        Integer totalCustomerCount = tCustomerMapper.selectTotalCustomerCount();
        BigDecimal customerNum = NumberUtil.div(new BigDecimal(totalCustomerCount), new BigDecimal(totalClueCount), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
        NameValueDataQuery customerData = NameValueDataQuery.builder()
                .name("客户")
                .value(customerNum)
                .build();
        nameValueDataQueryList.add(customerData);
        Integer totalTranCount = tTranMapper.selectTotalTranCount();
        BigDecimal tranNum = NumberUtil.div(new BigDecimal(totalTranCount), new BigDecimal(totalClueCount), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
        NameValueDataQuery tranData = NameValueDataQuery.builder()
                .name("交易")
                .value(tranNum)
                .build();
        nameValueDataQueryList.add(tranData);
        Integer successTranCount = tTranMapper.selectSuccessTranCount();
        BigDecimal successTranNum = NumberUtil.div(new BigDecimal(successTranCount), new BigDecimal(totalClueCount), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
        NameValueDataQuery successTranData = NameValueDataQuery.builder()
                .name("成交")
                .value(successTranNum)
                .build();
        nameValueDataQueryList.add(successTranData);
        return nameValueDataQueryList;
    }

    @Override
    public List<NameValueDataQuery> getCluePieData() {
        return tClueMapper.selectClueSourceCount();
    }
}
